<!--添加修改用户信息-->
<template>
    <a-modal v-model="showAddTable" :width="630"
             :title="title ? '数据字典新增' : '数据字典编辑'"
             :maskClosable="false"
             :destroyOnClose="true">
        <div>
            <a-row>
                <a-form ref="sysDictDTO" :form="form" @keydown.native.enter.prevent="saveApply" class="modelSearch">
                    <a-form-item label="系统名称：" v-bind="formStyle">
                        <a-select  v-model="serverCode"  style="width: 350px" disabled
                                   placeholder="全部">
                            <a-select-option v-for="item in nacosService" :value="item.serverCode" :key="item.serverCode">{{item.serverName }}
                            </a-select-option>
                        </a-select>
                    </a-form-item>
                        <a-form-item v-if="!input" label="类型：" v-bind="formStyle" required ref="sortCodeRef">
                            <a-select :disabled="!title"  style="width: 350px"
                                      placeholder="全部"  v-decorator="['sortCode',  { rules: [{ required: true, message: '请选择类型' }] }]">
                                <a-select-option v-for="item in sorts" :value="item.value" :key="item.value">{{
                                    item.label }}
                                </a-select-option>
                            </a-select>
                            <a-icon v-if="title" type="menu-unfold"
                                    style="margin-left: 7px;float:right;cursor:pointer;margin-top: 12px;"
                                    @click="changeInputType('sortCodeRef')"></a-icon>
                        </a-form-item>
                        <a-form-item v-if="title&&input" label="类型名称：" v-bind="formStyle" required ref="sortNameRef">
                            <!--<a-input v-model="sysDictDTO.sortName" allowClear style="width: 230px;" :maxlength="50" />-->
                            <a-input
                                    placeholder="请输入类型名称" allowClear style="width: 350px"
                                    v-decorator="[
                        'sortName',
                        {
                            rules: [
                                { required: true, message: '请输入汉字、字母、数字、_或-字符' },
                                { validator: this.verify.validateName.bind(this) }
                            ],
                            getValueFromEvent: event => {
                                return this.verify.formalSpace(event)
                            }
                        }
                    ]"
                            />
                            <a-icon type="menu-fold" style="margin-left: 7px;float:right;cursor:pointer; margin-top: 12px;"
                                    @click="changeInputType('sortNameRef')"></a-icon>
                        </a-form-item>
                        <a-form-item label="字典编码：" v-bind="formStyle" required>
                            <!--<a-input v-model="sysDictDTO.code" :disabled="!title" style="width: 350px;"
                                     :maxlength="16" />-->
                            <a-input
                                    placeholder="请输入字典编码" allowClear style="width: 350px"
                                    v-decorator="[
                        'code',
                        {
                            getValueFromEvent: event => {
                                return this.verify.formalSpace(event)
                            }
                        }
                    ]"
                            />
                        </a-form-item>
                        <a-form-item label="字典名称："  v-bind="formStyle" required>
                            <a-input  placeholder="请输入字典名称" style="width: 350px"
                                     v-decorator="[
                        'name',
                        {
                            getValueFromEvent: event => {
                                return this.verify.formalSpace(event)
                            }
                        }
                    ]"
                            />
                        </a-form-item>
                        <a-form-item label="排序：" v-bind="formStyle" required>
                            <a-input-number
                                    placeholder="请输入排序"
                                    :min="0"
                                    :max="999999"
                                    :formatter="verify.limitInteger"
                                    :parser="verify.limitInteger"
                                    style="width: 350px"
                                    @blur="leaveValue"
                                    v-decorator="['orderNum', { initialValue: '0' }, { rules: [{ required: true, message: '请输入正整数' }] }]"
                            ></a-input-number>
                        </a-form-item>
                </a-form>
            </a-row>
        </div>
        <div slot="footer">
            <a-button type="default" size="default" @click="cancelApply()">取消</a-button>
            <a-button type="primary" size="default" @click="saveApply()">确定</a-button>
        </div>
    </a-modal>
</template>

<script>
    import verify from 'libs/verify'
    export default {
        name: 'addEdit',
        data () {
            return {
                verify,
                form: this.$form.createForm(this),
                showAddTable: false,
                input: false,
                originalData: {},
                id:0
            }
        },
        computed: {
            formStyle() {
                return this.$store.state.common.formStyle
            }
        },
        props: {
            sysDictDTO: {
                type: Object,
                required: true
            },
            title: {
                type: Boolean,
                required: true
            },
            sorts: {
                type: Array,
                required: true
            },nacosService: {
                type: Array,
                required: true
            },serverCode: {
                type: String,
                required: true
            },
        },
        watch: {},
        methods: {
            leaveValue(e){
                let a =e.target.value
                if(a === '' || a=== undefined){
                    this.$nextTick(() => {
                        this.form.setFieldsValue({ orderNum: 0 })
                    })
                }
            },
            /**
             * Function    : saveApply
             * Description : 打开弹窗
             * Input       : name
             * Return      : none
             */
            showModal () {
                this.showAddTable = true
            },
            /**
             * Function    : showEdit
             * Description : 编辑时单位显示
             * Input       : opt单个单位所有数据
             * Return      : none
             */
            showEdit(opt) {
                this.showAddTable = true
                this.id = opt.id
                this.$nextTick(() => {
                    this.form.setFieldsValue(this.$pick(opt, ['sortCode', 'code', 'name','orderNum']))
                })
            },

            /**
             * Function    : saveApply
             * Description : 用户信息新增、编辑保存
             * Input       : name
             * Return      : none
             */
            saveApply () {
                this.form.validateFields((err, value) => {
                    if (!err) {
                        if (this.title) {
                            let para = Object.assign ({}, value)
                            para.serverCode = this.serverCode
                            this.$store.dispatch ('addSysDictAct', para).then (response => {
                                this.showAddTable = false
                                if (response.code === 0) {
                                    this.$emit ('init')
                                    this.$message.success (response.message)
                                    this.input = false
                                } else {
                                    this.$message.error (response.message)
                                }
                            })
                        } else {
                            if (this.originalData !== this.sysDictDTO) {
                                value.id = this.id
                                let para = Object.assign ({}, value)
                                para.serverCode = this.serverCode
                                this.$store.dispatch ('updSysDictAct', para).then (response => {
                                    this.showAddTable = false
                                    if (response.code === 0) {
                                        this.$emit ('init')
                                        this.$message.success (response.message)
                                        this.input = false
                                    } else {
                                        this.$message.error (response.message)
                                    }
                                })
                            } else {
                                this.showAddTable = false
                            }
                        }
                    }
                })
            },

            /**
             * Function    : cancelApply
             * Description : 取消弹出框
             * Input       : name
             * Return      : none
             */
            cancelApply ( name ) {
                this.showAddTable = false
                let _this =this
                setTimeout(function() {
                    _this.form.resetFields()
                }, 100)
                this.input = false
            },

            /**
             * Function    : changeInputType
             * Description : 输入框和select框切换
             * Input       : name
             * Return      : none
             */
            changeInputType ( name ) {
                this.input = !this.input
                let _this =this
                setTimeout(function() {
                    _this.form.resetFields()
                }, 100)
            }
        },

        created () {

        }
    }
</script>
